local jueyou = fk.CreateSkill {
  name = "lvshi__jueyou",
}

Fk:loadTranslationTable{
  ["lvshi__jueyou"] = "谲诱",
  [":lvshi__jueyou"] = "其他角色的回合开始时，若你在其攻击范围内，你可以摸一张牌，然后其此阶段只能使用牌指定你为目标；此回合结束时，若你未受到伤害，你可以将所摸的牌当【出其不意】对其使用。",
  ["@@jueyou_card-turn"] = "谲诱",
  ["#lvshi__jueyou-invoke"] = "谲诱：你可以发动谲诱，摸一张牌并限制 %src 此阶段只能指定你为目标",
  ["#lvshi__jueyou-chuqibuyi"] = "谲诱：你可以将摸到的牌当【出其不意】对 %src 使用",
  ["$lvshi__jueyou1"] = "良禽当择木而息，纺愿笺书乞降！",
  ["$lvshi__jueyou2"] = "愿以鄱阳归魏，请将军派兵接应",
}

-- 记录谲诱状态的数据结构
local jueyou_data = {}

jueyou:addEffect(fk.TurnStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(jueyou.name) and target ~= player then
      return target:inMyAttackRange(player)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = jueyou.name,
      prompt = "#lvshi__jueyou-invoke:"..target.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:drawCards(player, 1, jueyou.name)
    room:setPlayerMark(target, "jueyou_target-turn", player.id)
    room:setCardMark(Fk:getCardById(card[1]), "@@jueyou_card-turn", player.id)
    room:setPlayerMark(player, "jueyou_card-turn", card[1])
  end,
})

-- 回合结束时触发出其不意效果
jueyou:addEffect(fk.TurnEnd, {
  anim_type = "offensive",
  --is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(jueyou.name) or player.dead then return end
    if target:getMark("jueyou_target-turn") == player.id then
      local id = player:getMark("jueyou_card-turn")
      if table.contains(player:getCardIds("he"), id) then
        return #player.room.logic:getActualDamageEvents(1, function (e)
        if (e.data.from and e.data.from == target and e.data.to and e.data.to == player) then
          return true
        end
      end, Player.HistoryTurn) == 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = jueyou.name,
      prompt = "#lvshi__jueyou-chuqibuyi:"..target.id,
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local id = player:getMark("jueyou_card-turn")
    room:useVirtualCard("lvshi__unexpectation", id, player, target, jueyou.name)
  end,
})

jueyou:addEffect("prohibit", {
  is_prohibited = function(self, from, to, card)
    if from:getMark("jueyou_target-turn") ~= 0 then
      return from:getMark("jueyou_target-turn") ~= to.id
    end
  end,
})

return jueyou